<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of StoreForm
 *
 * @author WIN7
 */
class StoreForm extends CFormModel
{
        public $name;
        public $mobile;
        public $address;
        public $lat;
        public $lng;
        public $image;
        /**
	 * Declares the validation rules.
	 * 
	 * 
	 */
	public function rules()
	{
		return array(
			
			array('name, mobile, address, lat, lng', 'required'),
                                                         array('image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update', 'maxSize'=>41943040 ,'tooLarge'=>'The file was larger than 4MB. Please upload a smaller file.'),
                                                         array('title, image', 'length', 'max'=>255, 'on'=>'insert,update'),
		);
	}

	/**
	 * Declares attribute labels.
	 */
        
        public function attributeLabels()
	{		
	}
                    
	/**
	 */
	public function add(){              
                        try{
                                 $connection=Yii::app()->db;   // assuming you have configured a "db" connection  
                                 $query="INSERT INTO os_store(store_name,store_mobile,store_address,store_lat,store_lng,store_image,sale_id) 
                                     VALUE('{$this->name}','{$this->mobile}','{$this->address}','{$this->lat}','{$this->lng}','{$this->image}',".Yii::app()->session["saleId"].")";
                                 $connection->createCommand($query)->query();   
                                 return true;
                       }catch(Exception $e){
                                 $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                                 echo $string=json_encode($result);
                                 return FALSE;
                                 }                    
                        }
                        public function edit(){              
                        try{
                                 $connection=Yii::app()->db;   // assuming you have configured a "db" connection  
                                 //find store_id from rowId in jqGrid
                                 $query_id="SELECT os_store.store_id, os_store.store_image FROM os_store
                                     WHERE os_store.sale_id =".Yii::app()->session["saleId"];
                                 $command_id = $connection->createCommand($query_id);
                                 $dataReader_id=$command_id->query();   
                                 $rows_store = array();
                                while(($row=$dataReader_id->read())!==false) {
                                    $rows_store[]=$row;
                                }
                                //store_id in os_store table;
                                $rowId = $_GET['id']-1;
                                $store_id = $rows_store[$rowId]['store_id'];
                                //update to database
                                if (!empty($rows_store[$_GET['id']-1]['store_image']) && empty($this->image)) {
                                $this->image = $rows_store[$_GET['id']-1]['store_image'];
                                }
                                $query="UPDATE os_store SET os_store.store_name='{$this->name}', os_store.store_mobile='{$this->mobile}',os_store.store_address='{$this->address}', os_store.store_lat='{$this->lat}', os_store.store_lng='{$this->lng}', os_store.store_image='{$this->image}' WHERE os_store.store_id=".$store_id; 
                                $command=$connection->createCommand($query)->execute();
//                                $command->bindParam(":name",  $this->name,PDO::PARAM_STR);
//                                $command->bindParam(":mobile",  $this->mobile,PDO::PARAM_STR);
//                                $command->bindParam(":address",  $this->address,PDO::PARAM_STR);
//                                $command->bindParam(":lat",  $this->lat,PDO::PARAM_STR);
//                                $command->bindParam(":lng",  $this->lng,PDO::PARAM_STR);
//                                $command->bindParam(":image",  $this->image,PDO::PARAM_STR);
//                                $command->bindParam(":id",$store_id,PDO::PARAM_INT);
//                                $row_count=$command->execute();
//                                $resul=array('data'=>$json,'status'=>'ok');
//                                $string=json_encode($resul);
//                                echo $string;                                                              
                                 return true;
                                
                       }catch(Exception $e){
                                 $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                                 echo $string=json_encode($result);
                                 return FALSE;
                                 }                    
                        }
}


?>
